/**
 * Created by Lwang on 2017-04-05.
 */
require("angular");
import Util from "utils"
import layer from "layer-mobile"

export default function () {
    var serviceModule = angular.module("serviceModule", []);
    /**
     * 处理http response status code
     */
    serviceModule.factory("Http", ["$http", "$state", function ($http, $state) {
        return {
            /**
             * ajax请求封装
             * @param opt   config parameters
             *         @attribute url       String   Required
             *         @attribute method    String   Optional
             *         @attribute data      Object   Optional
             *         @attribute success   Function Required
             *         @attribute error     Function Required
             * @returns {HttpPromise}
             */
            request: function (opt) {
                // var showLoading = opt.showLoading !== false;
                var cover = layer.open({
                    type: 2,
                    content: "正在加载中...",
                });
                return $http({
                    url: opt.url,
                    method: opt.method || "POST",
                    xhrFields: {withCredentials: true},
                    headers: {
                        'Content-Type': 'application/json',
                        'accept': '*/*'
                    },
                    data: Util.serialize(opt.data || {})
                }).success(function (res) {
                    var respone = res;
                    if (Util.getType(respone) != "object" && Util.getType(respone) != "array") {
                        respone = {
                            result: res
                        }
                    }
                    opt.success(respone);
                }).error(function (err) {
                    opt.error(err);

                    // layer.closeAll();
                });
            },

            /**
             * Http response handler
             * @param opt   config parameters
             *          @attribute  status      String          Required
             *          @attribute  showTip     Boolean         Optional
             *          @attribute  message     String          Optional
             *          @attribute  success     Function        Optional
             *          @attribute  exception   Function        Optional
             *          @attribute  error       Function        Optional
             *          @attribute  unlogin       Function        Optional
             */
            handler: function (opt) {
                // var showTip = opt.showTip !== false;
                if (opt.code) {
                    switch (opt.code) {
                        case 100:
                            layer.open({
                                type: 1,
                                content: opt.message,
                                skin:"msg",
                                time:2
                            });
                            break;
                        case 10000:
                            var name = $state.current.name;
                            if(name.indexOf("productList") != -1 || name.indexOf("listDetail") != -1){
                                if (Util.getType(opt.success) === "function") {
                                    opt.success(opt);
                                    layer.closeAll();
                                }
                            }else{
                                $state.go("tips1");
                            }
                            break;
                        default:
                            if (Util.getType(opt.error) === "function") {
                                opt.error(opt);
                            }
                            break;
                    }
                } else {
                    if (Util.getType(opt.success) === "function") {
                        opt.success(opt);
                        layer.closeAll();
                    }
                }

            }
        };
    }]);
}


